package meta.api.web.operation.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OpnMngDao {

    @Autowired
    private SqlSessionTemplate sqlSession;

    private static final String NAMESPACE = "meta.api.web.operation.dao.opnMng.";

    public int getValueTotal(String type, String queryForKey) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("queryForKey", queryForKey);
        return sqlSession.selectOne(NAMESPACE + "getValueTotal", params);
    }

    public List<String> getValueList(String type, String queryForKey,
            Integer current, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("queryForKey", queryForKey);
        if (current != null && pageSize != null) {
            params.put("current", pageSize * (current - 1));
            params.put("pageSize", pageSize);
        } else {
            params.put("current", null);
            params.put("pageSize", null);
        }
        return sqlSession.selectList(NAMESPACE + "getValueList", params);
    }

    public boolean checkExistValue(String type, String key) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("key", key);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkExistValue", params);
        return result > 0 ? true : false;
    }

    public void insertValue(String type, String key, String value) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("key", key);
        params.put("value", value);
        sqlSession.insert(NAMESPACE + "insertValue", params);
    }

    public void updateValue(String type, String key, String value) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("key", key);
        params.put("value", value);
        sqlSession.update(NAMESPACE + "updateValue", params);
    }

    public void deleteValue(String type, String key) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("key", key);
        sqlSession.delete(NAMESPACE + "deleteValue", params);
    }
}
